package com.dropdb.demo.controller.admin;

import com.dropdb.demo.bean.Goods;
import com.dropdb.demo.bean.Groupon;
import com.dropdb.demo.bean.GrouponRules;
import com.dropdb.demo.bean.bo.GrouponRuleCreateBO;
import com.dropdb.demo.bean.vo.BaseData;
import com.dropdb.demo.bean.vo.BaseRespVO;
import com.dropdb.demo.mapper.GoodsMapper;
import com.dropdb.demo.service.PromotionService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author qugr
 * @create 2020-06-26 16:53
 */
@RestController
@RequestMapping("admin/groupon")
public class AdminPromotionGrouponController {

    @Autowired
    PromotionService promotionService;
    @Autowired
    GoodsMapper goodsMapper;

    @RequestMapping("list")
    @RequiresPermissions(value = {"*", "admin:groupon:list"}, logical = Logical.OR)
    public BaseRespVO queryGrouponRules(Integer page, Integer limit, Integer goodsId, String sort, String order){
        BaseData baseData = promotionService.queryGrouponRules(page, limit, goodsId, sort, order);
        return BaseRespVO.ok(baseData);
    }

    @RequestMapping("listRecord")
    @RequiresPermissions(value = {"*", "admin:groupon:read"}, logical = Logical.OR)
    public BaseRespVO queryGroupon(Integer page, Integer limit, Integer goodsId, String sort, String order){
        BaseData baseData = promotionService.queryGroupon(page, limit, goodsId, sort, order);
        return BaseRespVO.ok(baseData);
    }

    @RequestMapping("create")
    @RequiresPermissions(value = {"*", "admin:groupon:create"}, logical = Logical.OR)
    public BaseRespVO createGrouponRule(@RequestBody GrouponRuleCreateBO grouponRuleBO){
        Goods goods = goodsMapper.selectByPrimaryKey(grouponRuleBO.getGoodsId());
        if (goods == null) {
            return BaseRespVO.error(500,"参数错误，商品不存在");
        }
        int insert = promotionService.insertGrouponRule(goods,grouponRuleBO);
        if (insert != 0) {
            GrouponRules grouponRuleVO = promotionService.queryGrouponRule(grouponRuleBO.getId());
            return BaseRespVO.ok(grouponRuleVO);
        }
        return BaseRespVO.error(500,"添加失败");
    }

    @RequestMapping("delete")
    @RequiresPermissions(value = {"*", "admin:groupon:delete"}, logical = Logical.OR)
    public BaseRespVO deleteGrouponRule(@RequestBody GrouponRules grouponRule){
        int delete = promotionService.deleteGrouponRule(grouponRule.getId());
        if (delete != 1) {
            return BaseRespVO.error(500,"删除失败");
        }
        return BaseRespVO.ok();
    }


    @RequestMapping("update")
    @RequiresPermissions(value = {"*", "admin:groupon:update"}, logical = Logical.OR)
    public BaseRespVO updateGrouponRule(@RequestBody GrouponRules grouponRule){
        Goods goods = goodsMapper.selectByPrimaryKey(grouponRule.getGoodsId());
        if (goods == null) {
            return BaseRespVO.error(500,"参数错误，商品不存在");
        }

        int update = promotionService.updateGrouponRule(goods,grouponRule);
        if (update != 1) {
            return BaseRespVO.error(500,"更新失败");
        }
        GrouponRules queryGrouponRule = promotionService.queryGrouponRule(grouponRule.getId());
        return BaseRespVO.ok(queryGrouponRule);
    }

}
